﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ARCam3D.Imaging.Processing
{
    public class VectorImageBinarizator
    {
        private double threshold = 300;

        unsafe public BinaryImage Binarizate(VectorImage image)
        {
            BinaryImage result = new BinaryImage(image.Width, image.Height);

            int componentsCount = VectorImage.ComponenetsCount;
            int xOffset = VectorImage.XOffset;
            int yOffset = VectorImage.YOffset;

            long pointsCount = image.Width * image.Height;
            int* dataPointer = (int*)image.DataPtr.ToPointer();
            byte* resultDataPointer = (byte*)result.DataPtr.ToPointer();
            for (long point = 0; point < pointsCount;
                ++point, dataPointer += componentsCount, ++resultDataPointer)
            {
                double length = Math.Sqrt(dataPointer[xOffset] * dataPointer[xOffset] + dataPointer[yOffset] * dataPointer[yOffset]);
                *resultDataPointer = (byte)(length > threshold ? 0xFF : 0);
            }

            return result;
        }
    }
}
